package ext.tianma.part.report.searchAPL.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wt.csm.navigation.ClassificationNode;
import wt.csm.navigation.litenavigation.ClassificationNodeDefaultView;
import wt.csm.navigation.service.ClassificationObjectsFactory;
import wt.fc.ReferenceFactory;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTRuntimeException;
import ext.tianma.part.finishedProduct.util.CSMUtil;

public class APLConfig {

	private static String FILEPATH ;
	private static File file ;
	private static Long lastModifyTime = 0L;
	private static String THOME ;
	
	private static Properties prop = new Properties();
	private static final String LIBRARYNAME = "libraryName";//指定存储库下的外发资料，没有【下一步】操作
	
	private static final Logger logger = LoggerFactory.getLogger(APLConfig.class);
	
	static {
		try {
			WTProperties wtproperties = WTProperties.getLocalProperties();
			THOME = wtproperties.getProperty("wt.codebase.location");
			FILEPATH = THOME + File.separator + "ext" +
					File.separator + "tianma" + File.separator + "part" + File.separator +
					"report" + File.separator + "searchAPL" + File.separator + "config" + File.separator + "APLConfig.properties";
			
			file = new File(FILEPATH);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getKey(String key) throws DocumentException, IOException {
		load();
		String value = prop.getProperty(key);
		return value;
	}
	
	/**
	 *
	 * @param key
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 * @author xucr
	 * @throws WTException 
	 * @CreateDate 2020年7月2日
	 */
	public static List<String> getKeyToList(String key) throws DocumentException, IOException {
		String value = getKey(key);
		List<String> res = null;
		if(StringUtils.isNotEmpty(value)) {
			res = Arrays.asList(value.split(","));
		} else {
			res = new ArrayList<String>();
		}
		return res;
	}
	
	/**
	 * 获取配置文件中的信息
	 * @param nodeOid 分类节点oid
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 * @throws WTRuntimeException
	 * @throws WTException
	 * @author xucr
	 * @CreateDate 2020年7月2日
	 */
	public static List<String> getKeyToListByNode(String nodeOid) throws DocumentException, IOException, WTRuntimeException, WTException {
		/**
		 * 配置文件中的配置格式：
		 * 分类完整路径=需要展示的分类属性key
		 * */
		ReferenceFactory rf = new ReferenceFactory();
		ClassificationNode node = (ClassificationNode) rf.getReference(nodeOid).getObject();
		ClassificationNodeDefaultView cndv = ClassificationObjectsFactory.newClassificationNodeDefaultView(node);
    	String fullPath = CSMUtil.getClassificationFullPath(cndv);
    	List<String> res = getKeyToList(fullPath);
		return res;
	}
	
	private static Properties load() throws DocumentException, IOException {
		logger.info("FILEPATH>>>" + FILEPATH);
		if(file.lastModified() > lastModifyTime) {
			prop.clear();
			InputStream ins = new FileInputStream(FILEPATH);
			if(null != ins) {
				InputStreamReader inputst = new InputStreamReader(ins, "UTF-8");
				prop.load(inputst);
			}
			lastModifyTime = file.lastModified();
		}
		logger.info("prop>>>" + prop);
		return prop;
	}
}
